﻿using System.Linq;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model.ViewModel.Product;
using ZBJF.Warranty.WebProvider.Filter;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class CommonController : Controller
    {
        [LoginFilter(IsCheck = false)]
        [AuthenticationFilter(NeedAuthentication = false)]
        public ActionResult Redirect()
        {
            return View();
        }
        public JsonResult GetProvinceList()
        {
            var provinceList = ServiceFactory.D_ProvinceServiceInstance.GetDProvinceAll();
            var selectList = new SelectList(provinceList, "ProvinceID", "ProvinceName");
            return Json(selectList, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetCityList(int provinceID)
        {
            var cityList = ServiceFactory.D_CityServiceInstance.GetCityListByProvinceID(provinceID);
            var selectList = new SelectList(cityList, "CityID", "CityName");
            return Json(selectList, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetProductModelList(int brandId)
        {
            var modelList = ServiceFactory.ProductModelsServiceInstance.GetProductModel(brandId);
            var selectList = new SelectList(modelList, "ID", "ModelName");
            return Json(selectList, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetProductBrandList()
        {
            var brandName = (Request["q"] ?? "").ToLower();
            var modelList = ServiceFactory.ProductBrandsServiceInstance.GetBrandsLikeName(brandName).ToList();
            var selectList = new SelectList(modelList, "ID", "BrandName");
            return Json(selectList, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetProductModelListByBrandId(int brandId)
        {
            var modelList = ServiceFactory.ProductModelsServiceInstance.GetProductModel(brandId);
            var selectList = new SelectList(modelList, "ID", "ModelName");
            return Json(selectList, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetProductModelListByBrandIdAndModelName()
        {
            var modelName = (Request["q"] ?? "").ToLower();
            var productModel = new ProductModelsModel
            {
                ModelName = modelName,
                BrandId = int.Parse(string.IsNullOrEmpty(Request["BrandId"]) ? "0" : Request["BrandId"])
            };
            var modelList = ServiceFactory.ProductModelsServiceInstance.GetProductModel(productModel).ToList();
            var selectList = new SelectList(modelList, "ID", "ModelName");
            return Json(selectList, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [AuthenticationFilter(NeedAuthentication = false)]
        public ActionResult RemoveAllCache()
        {
            CacheHelper.Instance.RemoveAllCache();
            return View();
        }
    }
}